package com.atguigu.yygh.hosp.receiver;

import com.atguigu.yygh.common.component.MsmSender;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.msm.MsmVo;
import com.atguigu.yygh.vo.order.OrderMqVo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 孔德成
 * @slogan 致敬大师，致敬未来的你
 * @date 2021/9/14 2:31 上午
 * @desc
 */
@Component
@RabbitListener(queues = "yygh.order.cancel")
public class CancelOrderReceiver {
    private final ScheduleService scheduleService;

    private final MsmSender msmSender;

    public CancelOrderReceiver(ScheduleService scheduleService, MsmSender msmSender) {
        this.scheduleService = scheduleService;
        this.msmSender = msmSender;
    }

    @RabbitHandler
    public void receiver(OrderMqVo orderMqVo) {
        //取消预约成功更新预约数
        Schedule schedule = scheduleService.getScheduleById(orderMqVo.getScheduleId());
        // 剩余预约数量加1
        schedule.setAvailableNumber(schedule.getAvailableNumber() + 1);
        scheduleService.update(schedule);
        //发送短信
        MsmVo msmVo = orderMqVo.getMsmVo();
        if (null != msmVo) {
            msmSender.sendMessage(msmVo);
        }
    }
}
